package com.sunny.ynhz.utils;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * Created by huhuan123 on 2018/8/7.
 *
 * @describe TODO
 */

public class MoveToPosition {
    /**
     * RecyclerView 移动到当前位置，
     *
     * @param manager       设置RecyclerView对应的manager
     * @param mRecyclerView 当前的RecyclerView
     * @param n             要跳转的位置
     */
    public static void moveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {
        int firstItem = manager.findFirstVisibleItemPosition();
        int lastItem = manager.findLastVisibleItemPosition();
        if (n <= firstItem) {
            mRecyclerView.scrollToPosition(n);
        } else if (n <= lastItem) {
            int top = mRecyclerView.getChildAt(n - firstItem).getTop();
            mRecyclerView.scrollBy(0, top);
        } else {
            mRecyclerView.scrollToPosition(n);
        }
    }


    /**
     * RecyclerView 移动到当前位置，
     *
     * @param manager       设置RecyclerView对应的manager
     * @param mRecyclerView 当前的RecyclerView
     * @param n             要跳转的位置
     */
    public static void moveToPositionHor(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {
        if (n <= 2) {
//            int firstItem = manager.findFirstVisibleItemPosition();
//            int lastItem = manager.findLastVisibleItemPosition();
//            if (n <= firstItem) {
//                mRecyclerView.scrollToPosition(n);
//            } else if (n <= lastItem) {
//                int top = mRecyclerView.getChildAt(n - firstItem).getLeft();
//                mRecyclerView.scrollBy(top, 0);
//            } else {
                mRecyclerView.scrollToPosition(0);
//            }

        } else {
            n = n - 2;
            int firstItem = manager.findFirstVisibleItemPosition();
            int lastItem = manager.findLastVisibleItemPosition();
            if (n <= firstItem) {
                mRecyclerView.scrollToPosition(n);
            } else if (n <= lastItem) {
                int top = mRecyclerView.getChildAt(n - firstItem).getLeft();
                mRecyclerView.scrollBy(top, 0);
            } else {
                mRecyclerView.scrollToPosition(n);
            }
        }

    }

}